/**
 * project: finance-manage
 * package: com.example.services
 * 服务处理 > 用户处理
 */

package com.example.services;

import com.example.controllers.AccountControl;
import com.example.interfaces.AccountServiceInterface;
import com.example.models.JsonModel;
import com.example.util.Debug;

public class AccountService extends Service_Base implements AccountServiceInterface {
    /* 逻辑处理类 */
    private final AccountControl accountControl;

    public AccountService(String requestJsonStr) {
        super(requestJsonStr);
        accountControl = new AccountControl();
    }

    /* 执行 */
    @Override
    public String run() {
        return switch (action) {
            case "register" -> register();
            case "login" -> login();
            case "change" -> changePwd();
            default -> null;
        };
    }

    /* 注册 */
    @Override
    public String register() {
        String username = (String) requestData.get("username");
        String password = (String) requestData.get("password");
        JsonModel responseJson = accountControl.register(username, password);
        return responseJson.toString();
    }

    /* 登录 */
    @Override
    public String login() {
        String username = (String) requestData.get("username");
        String password = (String) requestData.get("password");
        Debug.print(getClass(), ":", username, password);
        JsonModel responseJson = accountControl.login(username, password);
        return responseJson.toString();
    }

    /* 修改密码 */
    @Override
    public String changePwd() {
        String username = (String) requestData.get("username");
        String pic = (String) requestData.get("pic");
        String oldPwd = (String) requestData.get("oldPwd");
        String newPwd = (String) requestData.get("newPwd");

        JsonModel responseJson = accountControl.changePwd(username, pic, oldPwd, newPwd);
        return responseJson.toString();
    }

    @Override
    public void close() {
        this.accountControl.close();
    }

}